package cc.linker.steplink.crm.service.impl;

import cc.linker.steplink.crm.dao.CustomerDao;
import cc.linker.steplink.crm.dao.RobotSurveyDao;
import cc.linker.steplink.crm.service.RobotSurveyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RobotSurveyServiceImpl implements RobotSurveyService {

    @Autowired
    private RobotSurveyDao robotSurveyDao;
    @Autowired
    private CustomerDao customerDao;

    @Override
    public List<Map<String, Object>> getArticleStatisticsByOrgId(String orgId) {
        return robotSurveyDao.getArticleStatisticsByOrgId(orgId);
    }

    @Override
    public List<Map<String, Object>> getLabelPercentageByOrgId(String orgId) {
        // 前五`sum`,labelId,labelName
        List<Map<String, Object>> labelPercentage = robotSurveyDao.getLabelPercentageByOrgId(orgId);
        // 总人数
        Integer count = customerDao.getCustomerCountByOrgId(orgId);
        for (Map<String, Object> map : labelPercentage) {
            Long integer = (Long) map.get("sum");
            if (integer == null || integer == 0) {
                map.put("ratio", 0);
            } else {
                double v = integer.doubleValue() / count.doubleValue() * 100;
                map.put("ratio", new BigDecimal(v).setScale(2, BigDecimal.ROUND_HALF_UP));
            }
        }
        Integer others = robotSurveyDao.getOthersLabelPercentageByOrgId(orgId);
        Map<String, Object> map = new HashMap<>(4);
        map.put("sum", others == null ? 0 : others);
        if (others == null || others == 0) {
            map.put("ratio", 0);
        } else {
            double v = others.doubleValue() / count.doubleValue() * 100;
            map.put("ratio", new BigDecimal(v).setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        map.put("labelId", "-");
        map.put("labelName", "其他");
        labelPercentage.add(map);
        return labelPercentage;
    }

    @Override
    public PageInfo getMarketProfileByOrgId(Map<String, Object> map) {
        String orgId = (String) map.get("orgId");
        Integer pageIndex = (Integer) map.get("pageIndex");
        Integer pageSize = (Integer) map.get("pageSize");
        if (pageIndex == null || pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex, pageSize);
        // 获取营销概况:composeId,composeName,readTimes,buyTimes,marketTimes,labelId
        List<Map<String, Object>> marketProfile = robotSurveyDao.getMarketProfileByOrgId(orgId);
        for (Map<String, Object> map1 : marketProfile) {
            // 求转化率=订单数/营销次数（次数）
            Integer buyTimes = Integer.valueOf(map1.get("buyTimes").toString());
            Integer marketTimes = Integer.valueOf(map1.get("marketTimes").toString());
            double rate;
            if (marketTimes == 0) {
                rate = 0;
            } else {
                rate = buyTimes.doubleValue() / marketTimes.doubleValue() * 100;
            }
            rate = new BigDecimal(rate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            map1.put("rate", rate + "%");
            // 获取类别
            String labelId = (String) map1.get("labelId");
            if (!StringUtils.isEmpty(labelId)) {
                String[] split = labelId.split(",");
                String labelNames = robotSurveyDao.getLabelNamesByLabelIds(split);
                map1.put("labelName", labelNames);
            } else {
                map1.put("labelName", "");
            }
        }
        return new PageInfo<>(marketProfile);
    }

    @Override
    public List<Map<String, Object>> getDealDataByOrgId(String orgId) {
        return robotSurveyDao.getDealDataByOrgId(orgId);
    }

    @Override
    public void addToDealData(Map<String, Object> map) {
        robotSurveyDao.addToDealData(map);
    }

    @Override
    public Integer judgeFirstTimeByCidOidAndType(Map<String, Object> map) {
        return robotSurveyDao.judgeFirstTimeByCidOidAndType(map);
    }

    @Override
    public Integer judgeArticleSendWithinOneDay(Map<String, Object> map) {
        return robotSurveyDao.judgeArticleSendWithinOneDay(map);
    }
}
